package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponMapper;
import com.qf.mapper.DtsCouponUserMapper;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DtsCouponServiceImpl implements DtsCouponService {

    @Autowired
    private DtsCouponMapper couponMapper;
    @Autowired
    private DtsCouponUserMapper couponUserMapper;
    @Override
    public List<DtsCoupon> readCoupon(Integer id) {
        QueryWrapper<DtsCoupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);

        List<DtsCoupon> list = couponMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public IPage<DtsCoupon> queryBrandPage(Integer id, String name, Integer page, Integer limit, String sort, String order) {

        IPage<DtsCoupon> ipage = new Page<>(page, limit);

        QueryWrapper<DtsCoupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);

        if (!StringUtils.isEmpty(id)){
            queryWrapper.eq("id",id);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.eq("name",name);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(sort);
            } else {
                queryWrapper.orderByDesc(sort);
            }
        }

        IPage<DtsCoupon> resultPage = couponMapper.selectPage(ipage, queryWrapper);

        return resultPage;
    }

    @Override
    public IPage<DtsCouponUser> queryCouponUserPage(Integer page, Integer limit, String sort, String order) {
        Page<DtsCouponUser> ipage = new Page<>();
        QueryWrapper<DtsCouponUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);

        IPage<DtsCouponUser> resultPage = couponUserMapper.selectPage(ipage, queryWrapper);


        return resultPage;
    }


}
